package com.zjq.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjq.dao.impl.DetailDaoImpl;
import com.zjq.dao.impl.QiongjiatiaoDaoImpl;
import com.zjq.dao.impl.RoleDaoImpl;
import com.zjq.pojo.Detail;
import com.zjq.pojo.Qiongjiatiao;
import com.zjq.pojo.Role;

@Service("qingjiaService")
public class QingJiaService {
	@Autowired
	private DetailDaoImpl detaildao = null;
	@Autowired
	private QiongjiatiaoDaoImpl qingdao = null;
	@Autowired
	private RoleDaoImpl roledao = null;
	//写请假条
	public boolean write (String name,String teacher,String context,String begintime,String endtime){
		boolean is = false;
		Timestamp ts = new Timestamp((new Date()).getTime());
		try {
			Role r = roledao.findByName(name);
			Role tea = roledao.findByName(teacher);
			Qiongjiatiao qj = new Qiongjiatiao(r, context,1,ts,Timestamp.valueOf(begintime),Timestamp.valueOf(endtime));
			Detail d = new Detail(r, tea, null,1, ts);
			qingdao.add(qj);
			detaildao.add(d);
			is = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
		return is;
	}
	//学生查询本人请假条
	public Qiongjiatiao findMyQingja(String name){
		return qingdao.findByName(name);
	}
	//查询请假条
	public List<?> findStudentQingja(String teacher){
		return detaildao.findByNext(teacher);
	}
	//审批请假条
	public boolean Shenpi(String teacher, Integer yesorno, Integer id,String next) {
		// TODO Auto-generated method stub

		boolean is = false;
		Timestamp ts = new Timestamp((new Date()).getTime());
		try {
			Role tea = roledao.findByName(teacher);
			Role nexttea = roledao.findByName(next);
			Qiongjiatiao q = qingdao.findById(id);
			Role stu = q.getRole();
			Detail detail = new Detail(stu, nexttea, tea,yesorno, ts);
			detaildao.add(detail);
			if (yesorno==1) {//失败设置:修改请假条
				q.setState(2);
				qingdao.update(q);
				is=true;
			}else if(tea.getLevel()>=4&&yesorno==0){//成功设置
				q.setState(0);
				qingdao.update(q);
				is = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		}
		return is;
	}
}
